package diveplanner;

import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

/**
 * @author Bucatini
 *
 * DISCLAIMER:
 * This software is made by amateurs for a software design project. This dive planner 
 * must never be used to plan real dives. We, the authors, will not be held responsible 
 * for any damages or injury resulting in usage of this software for live usage. Instead, 
 * treat this software as an exercise in good software design and the exploration of what 
 * makes usable programs and interfaces.
 *
 *
 * 2014-11-14
 * Replaces "Main.java" for the graphical interfaces.
 *
 */


public class MainMenu extends JFrame {

    private JButton createNewDive;
    private JButton loadSavedDive;
    private JLabel depthLabel;

    private DepthListHandler mDepthListHandler;
    private TimeListHandler mTimeListHandler;

    private JComboBox diveDepth;
    private JComboBox diveTime;
    private JComboBox surfaceTime;
    private JComboBox nextDepth;
    //private JComboBox Time;

    JCheckBox secondDiveCheckBox;

    private String[] surfaceTimes = {"0", "1", "300"};
    private String[] diveTimes;
    private String[] diveDepths = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                                   "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                                   "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
                                   "31", "32", "33", "34", "35", "36", "37", "38", "39", "40",
                                   "41", "42"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainMenu frame1 = new MainMenu();
                    frame1.setVisible(true);


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }  //end of main() method

    /**
     * Create the frame.
     */
    public MainMenu() {
        ButtonHandler eHandler;
        setTitle("Bucatini Dive Planner");
        setAlwaysOnTop(true);

        eHandler = new ButtonHandler();

        setBounds(100, 100, 600, 450);
        Container pane = getContentPane();
        pane.setLayout(null);

        createNewDive = new JButton("Create New Dive Plan");
        createNewDive.setBounds(200, 105, 174, 75);
        createNewDive.setVisible(true);
        pane.add(createNewDive);
        createNewDive.addActionListener(eHandler);

        loadSavedDive = new JButton("Load Saved Dive Plan");
        loadSavedDive.setBounds(199, 216, 174, 75);
        loadSavedDive.setVisible(true);
        pane.add(loadSavedDive);
        loadSavedDive.addActionListener(eHandler);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public class ButtonHandler implements ActionListener {

        public void actionPerformed (ActionEvent e) {
            String buttonTitle = e.getActionCommand();
            if(buttonTitle.equals("Create New Dive Plan")){
                CreateNewDivePlan();
            }
            else if(buttonTitle.equals("Load Saved Dive Plan")){
                LoadSavedDivePlan();
            }
        }

    }

    public void CreateNewDivePlan(){

        getContentPane().removeAll();

        secondDiveCheckBox = new JCheckBox();
        secondDiveCheckBox.setText("Check for Second Dive");
        secondDiveCheckBox.setSize(200, 30);
        secondDiveCheckBox.setLocation(350, 30);

        depthLabel = new JLabel("Set Dive Depth(in meters)");
        depthLabel.setSize(150, 30);
        depthLabel.setLocation(30, 30);
        depthLabel.setVisible(true);

        diveDepth = new JComboBox(diveDepths);
        diveDepth.setMaximumRowCount(5);
        diveDepth.setSize(80, 30);
        diveDepth.setLocation(190, 30);
        diveDepth.setVisible(true);

        mDepthListHandler = new DepthListHandler();
        diveDepth.addItemListener(mDepthListHandler);

        getContentPane().add(depthLabel);
        getContentPane().add(diveDepth);
        getContentPane().add(secondDiveCheckBox);

        repaint();

    }

    public void LoadSavedDivePlan(){

        getContentPane().removeAll();

        JLabel savedDivesLabel;

        savedDivesLabel = new JLabel("Saved Dives", SwingConstants.CENTER);
        savedDivesLabel.setSize(150, 30);
        savedDivesLabel.setLocation(225, 50);
        savedDivesLabel.setVisible(true);
        getContentPane().add(savedDivesLabel);

        repaint();
    }

    public class DepthListHandler implements ItemListener {

        DiveTable mDiveTable = new DiveTable();

        public void itemStateChanged(ItemEvent e) {
            if(e.getSource() == diveDepth){

                timeListSelection(mDiveTable);

            }
            repaint();
        }

    }

    public class TimeListHandler implements ItemListener {

        DiveTable mDiveTable = new DiveTable();

        public void itemStateChanged(ItemEvent e) {
            if(e.getSource() == diveTime){

                isSecondDive(mDiveTable);

            }
            getContentPane().validate();
            repaint();
        }
    }

    public static boolean isInContainer (JComboBox comp) {
        return SwingUtilities.getAncestorOfClass(JComboBox.class, comp) != null;
    }

    public void timeListSelection(DiveTable mDiveTable) {

        JLabel surfaceTimeLabel;
        JLabel nextDepthLabel;
        JLabel timeLabel;

        /*if(isInContainer(diveTime)){
            int count = 0;
            getContentPane().remove(diveTime);
            count++;
            System.out.println(count);
        }*/

        int diveSelection = Integer.parseInt(diveDepths[diveDepth.getSelectedIndex()]);
        int maxTime = mDiveTable.maxTimeAllowed(diveSelection);
        diveTimes = new String[maxTime + 1];
        while(maxTime >= 0){
            diveTimes[maxTime] = Integer.toString(maxTime);
            --maxTime;
        }




        timeLabel = new JLabel("Set Dive Time(in minutes)");
        timeLabel.setSize(150, 30);
        timeLabel.setLocation(30, 90);
        timeLabel.setVisible(true);

        diveTime = new JComboBox(diveTimes);
        diveTime.setMaximumRowCount(5);
        diveTime.setSize(80, 30);
        diveTime.setLocation(190, 90);
        diveTime.setVisible(true);

        surfaceTimeLabel = new JLabel("Set Surface Time(in minutes)");
        surfaceTimeLabel.setSize(150, 30);
        surfaceTimeLabel.setLocation(30, 150);
        surfaceTimeLabel.setVisible(true);

        surfaceTime = new JComboBox(surfaceTimes);
        surfaceTime.setMaximumRowCount(2);
        surfaceTime.setSize(80, 30);
        surfaceTime.setLocation(190, 150);
        surfaceTime.setVisible(true);

        if(secondDiveCheckBox.isSelected()){
            nextDepthLabel = new JLabel("Set Depth of Second Dive");
            nextDepthLabel.setSize(150, 30);
            nextDepthLabel.setLocation(30, 210);

            nextDepth = new JComboBox(diveDepths);
            nextDepth.setMaximumRowCount(2);
            nextDepth.setSize(80, 30);
            nextDepth.setLocation(190, 210);

            getContentPane().add(nextDepthLabel);
            getContentPane().add(nextDepth);
        }



        mTimeListHandler = new TimeListHandler();
        diveTime.addItemListener(mTimeListHandler);
        diveDepth.addItemListener(mDepthListHandler);

        getContentPane().add(surfaceTimeLabel);
        getContentPane().add(surfaceTime);
        getContentPane().add(timeLabel);
        getContentPane().add(diveTime);
    }

    public void secondDiveSelection(DiveTable mDiveTable){















        revalidate();
        repaint();
    }

    public void isSecondDive(DiveTable mDiveTable) {

        int selectedSurfaceTime = Integer.parseInt(surfaceTimes[surfaceTime.getSelectedIndex()]);
        int selectedNextDepth = Integer.parseInt(surfaceTimes[surfaceTime.getSelectedIndex()]);
        int nitroMinutes = 10;
        int selectedDepth = Integer.parseInt(diveDepths[diveDepth.getSelectedIndex()]);
        int selectedTime = Integer.parseInt(diveTimes[diveTime.getSelectedIndex()]);
        int residualNitro = mDiveTable.residualNitro(selectedDepth, selectedTime, selectedSurfaceTime, selectedNextDepth, nitroMinutes);

    }

}
